package race;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import javax.imageio.ImageIO;

public class Car {

    private final double MAX_SPEED = 7.0;
    
    private int x, y;
    private boolean up, down, left, right;
    private boolean keysChanged;
    private double angle;
    private int turningDirection;
    private Shape car;
    private Line2D.Double line;
    private int oldX, oldY;
    private double speed;
    private Image sprite;
    
    private GamePanel gp;
    
    
    public Car(GamePanel gp){
        x = 20;
        y = 20;
        up = false;
        down = false;
        left = false;
        right = false;
        keysChanged = true;
        turningDirection = 0;
        this.gp = gp;
        speed = 0.0;
        
        try{
            sprite = ImageIO.read(this.getClass().getResourceAsStream("Auto.png"));
            sprite = sprite.getScaledInstance(sprite.getWidth(null)/8, sprite.getHeight(null)/8, Image.SCALE_SMOOTH);
        }catch(Exception ex){
            
        }
        
    }
    
    public void update(){
        if(keysChanged){
            if(left){
                turningDirection = -1;
            }else if(right){
                turningDirection = 1;
            }else{
                turningDirection = 0;
            }
            keysChanged = false;
        }
        
        if(up){
           
            if(speed < MAX_SPEED){
                speed += 0.2;
                if(speed > MAX_SPEED){
                    speed = MAX_SPEED;
                }
            }
            
            
        }else{
            speed -= 0.5;
            if(speed < 0.0){
                speed = 0.0;
            }
        }
        
        Rectangle r = new Rectangle(x,y,sprite.getWidth(null),sprite.getHeight(null));
            int newCenterX = (int)Math.floor((double)r.getCenterX()+Math.cos(angle)*speed);
            int newCenterY = (int)Math.floor((double)r.getCenterY()+Math.sin(angle)*speed);
            
            int newX = newCenterX - sprite.getWidth(null)/2;
            int newY = newCenterY - sprite.getHeight(null)/2;
            
            oldX = x;
            oldY = y;
            
            x = newX;
            y = newY;
        
        r = new Rectangle(x,y,sprite.getWidth(null),sprite.getHeight(null));
        
        if(r.intersectsLine(new Line2D.Double(0, 0, gp.getWidth(), 0)) ||
           r.intersectsLine(new Line2D.Double(0, gp.getHeight(), gp.getWidth(), gp.getHeight())) ||
           r.intersectsLine(new Line2D.Double(0, 0, 0, gp.getHeight())) ||
           r.intersectsLine(new Line2D.Double(gp.getWidth(), 0, gp.getWidth(), gp.getHeight())))
        {
            x = oldX;
            y = oldY;
        }
        
        angle += 0.05*(double)turningDirection;
       
        /*AffineTransform afx = new AffineTransform();
        afx.rotate(angle, r.getCenterX(), r.getCenterY());
        car = afx.createTransformedShape(r);
        
        line = new Line2D.Double(r.getCenterX(), r.getCenterY(),
                Math.floor((double)r.getCenterX()+Math.cos(angle)*15), 
                Math.floor((double)r.getCenterY()+Math.sin(angle)*15));*/
    }
    
    public void render(Graphics g){
        Graphics2D g2d = (Graphics2D) g;
        g2d.rotate(angle, x+sprite.getWidth(null)/2, y+sprite.getHeight(null)/2);
        g2d.drawImage(sprite, x, y, null);
        //g2d.draw(car);
        //g2d.draw(line);
    }

    public void setUp(boolean up) {
        this.up = up;
    }

    public void setDown(boolean down) {
        this.down = down;
        keysChanged = true;
    }

    public void setLeft(boolean left) {
        this.left = left;
        keysChanged = true;
    }
  
    public void setRight(boolean right){
        this.right = right;
        keysChanged = true;
    }
}
